﻿using CBS.APP;
using CBS.Desktop.FormPage;
using CBS.Model.ManageModel;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CBS.Desktop.ViewPartial
{
    /// <summary>
    /// StudentBonus.xaml 的交互逻辑
    /// </summary>
    public partial class StudentBonus : UserControl
    {
        public Pagination pager = new() { rows = 10, page = 1, sidx = nameof(StudentBonusViewModel.StudentName), sord = "asc" };
        public StudentBonus()
        {
            InitializeComponent();



            LoadCondation();

        }

        private async void LoadCondation()
        {
            btn_Search.IsEnabled = false;
            // 加载 ComboBox 数据
            await LoadDataAsync();

            // 查询 Grid 数据
            FlushGrid();

            btn_Search.IsEnabled = true;
        }

        private async Task LoadDataAsync()
        {
            // 使用 Task.WhenAll 等待所有 ComboBox 数据加载完成
            var tasks = new List<Task>
            {
                BindSemester(),
            };

            await Task.WhenAll(tasks);
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // 调用处理方法
                pager.page = 1;
                pg_DataList.PageIndex = 1;
                FlushGrid();
                e.Handled = true; // 如果需要，标记事件已处理
            }
        }
        private async Task BindSemester()
        {
            try
            {
                btn_Search.IsEnabled = false;
                var data = await Task.Run(() => SemesterApp.GetSelectList());

                data.Insert(0, new Model.UIModel.SemesterSelectModel { SemesterId = 0, SemesterName = "全部" });
                var anonymousArray = new[] {
                        new { DisplayName = "全部", SelectValue = -1 },
                        new { DisplayName = "是", SelectValue = 1 },
                        new { DisplayName = "否", SelectValue = 0 }
                    };
                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Semester.ItemsSource = data;

                    cb_IsSatisfyingCondition.ItemsSource = anonymousArray;

                    cb_IsSettled.ItemsSource = anonymousArray;
                }));
                cb_Semester.SelectedIndex = 0;
                cb_IsSatisfyingCondition.SelectedIndex = 0;
                cb_IsSettled.SelectedIndex = 0;
                FlushGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void FlushGrid()
        {
            try
            {
                btn_Search.IsEnabled = false;

                string studentName = tb_Student.Text.Trim();
                string introduceStudentName = tb_IntroduceStudent.Text.Trim();
                int semesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
                int isSatisfyingCondition = int.Parse(cb_IsSatisfyingCondition.SelectedValue.ToString());
                int isSettled = int.Parse(cb_IsSettled.SelectedValue.ToString());

                var data = await Task.Run(() => StudentBonusApp.GetList(pager, studentName, introduceStudentName, semesterId));

                this.Dispatcher.Invoke(new Action(() =>
                {
                    pg_DataList.MaxPageCount = pager.total;
                    dgr_DataList.ItemsSource = data;
                    btn_Search.IsEnabled = true;
                }));
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
                btn_Search.IsEnabled = true;
            }

        }

        private void pg_DataList_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
        {
            pager.page = e.Info;
            FlushGrid();
        }


        private void btn_Search_Click(object sender, RoutedEventArgs e)
        {
            pager.page = 1;
            pg_DataList.PageIndex = 1;
            FlushGrid();
        }

        private void btn_BatchSettleWithStudentAmount_Click(object sender, RoutedEventArgs e)
        {
            //
        }

        private void dg_Bonus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (sender is Button button && button.DataContext is StudentBonusViewModel data)
                {
                    StudentBonusSettle bonus = new(data.StudentId)
                    {
                        Owner = Window.GetWindow(this)
                    };
                    var dialogResult = bonus.ShowDialog();
                    if (dialogResult.HasValue && dialogResult.Value)
                    {
                        FlushGrid();
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }
    }
}
